package rp;

import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import rp.group.LSNGroup;
import utils.COLOR;
import utils.UTIL;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CMDMain implements CommandExecutor {
    private Map<String, Boolean> conf;

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
        boolean isAdmin = sender.hasPermission("minecraft");
        boolean isPlayer = sender instanceof Player;
        Player player = isPlayer ? (Player) sender : null;
        Player target = null;

        if (args.length == 0)
            args = new String[]{"menu"};
        if (getCmd(args[0]) == "menu") {
            if (isPlayer) {
                player.openInventory(UTIL.MENU("主菜单", 6,
                        new HashMap<Integer, HashMap>() {{
                            put(0, new HashMap() {{
                                put("NAME", COLOR.GOLD.getValue() + "时钟");
                                put("MATERIAL", Material.CLOCK);
                                put("LORE", new ArrayList<String>() {{
                                    add(COLOR.CYAN.getValue() + "点击" + COLOR.GREEN.getValue() + "查看主世界时间");
                                }});
                                put("CLICK", "/r clock");
                            }});
                            put(8, new HashMap() {{
                                put("NAME", COLOR.RED.getValue() + "×");
                                put("MATERIAL", Material.BARRIER);
                                put("LORE", new ArrayList<String>() {{
                                    add(COLOR.CYAN.getValue() + "左击" + COLOR.GREEN.getValue() + "退出主菜单");
                                    add(COLOR.CYAN.getValue() + "右击" + COLOR.GREEN.getValue() + "查看帮助");
                                }});
                                put("RIGHT", "/r help");
                            }});
                            put(53, new HashMap() {{
                                put("NAME", COLOR.GREEN.getValue() + "末影箱");
                                put("MATERIAL", Material.ENDER_CHEST);
                                put("LORE", new ArrayList<String>() {{
                                    add(COLOR.CYAN.getValue() + "点击" + COLOR.GREEN.getValue() + "打开随身末影箱");
                                }});
                                put("CLICK", "/r enderchest");
                            }});
                            put(45, new HashMap() {{
                                put("NAME", COLOR.GREEN.getValue() + "坐标");
                                put("MATERIAL", Material.COMPASS);
                                put("LORE", new ArrayList<String>() {{
                                    add(COLOR.CYAN.getValue() + "点击" + COLOR.GREEN.getValue() + "查看当前坐标");
                                }});
                                put("CLICK", "/r position");
                            }});
                        }}
                ));
                return true;
            }
            args = new String[]{"help"};
        }
        if (getCmd(args[0]) == "help") {
            UTIL.SEND(sender, new String[]{
                    COLOR.GOLD.getValue() + "/r help" + COLOR.WHITE.getValue() + " - 获取指令列表及帮助。",
                    COLOR.GOLD.getValue() + "/r menu" + COLOR.WHITE.getValue() + " - 打开主菜单。",
                    COLOR.GOLD.getValue() + "/r clock" + COLOR.WHITE.getValue() + " - 查看主世界时间。",
                    COLOR.GOLD.getValue() + "/r position" + COLOR.WHITE.getValue() + " - 查看玩家坐标。",
                    COLOR.GOLD.getValue() + "/r enderchest" + COLOR.WHITE.getValue() + " - 打开玩家末影箱。",
                    COLOR.GOLD.getValue() + "/r inventory" + COLOR.WHITE.getValue() + " - 打开玩家物品栏。",
                    COLOR.GOLD.getValue() + "/r list" + COLOR.WHITE.getValue() + " - 显示玩家列表。",
            });
            return true;
        }
        switch (getCmd(args[0])) {
            case "clock":
                if (args.length > 1 && isAdmin) {
                    player.chat("/time set " + setTime(Integer.parseInt(args[1].replace(":", ""))));
                    UTIL.SEND(sender, COLOR.GOLD.getValue() + "操作成功。");
                }
                int h = getTime() / 100;
                int m = getTime() % 100;
                UTIL.SEND(sender, COLOR.GOLD.getValue() + "当前时间: " + COLOR.WHITE.getValue() + (h < 10 ? "0" + h : "" + h) + ":" + (m < 10 ? "0" + m : "" + m));
                return true;
            case "position":
                target = player;
                if (args.length > 1 && isAdmin){
                    target = Bukkit.getPlayer(args[1]);
                }
                if (target == null) {
                    UTIL.SEND(sender, COLOR.RED.getValue() + "操作失败，目标不存在。");
                    return true;
                }
                Location location = target.getLocation();
                UTIL.SEND(sender, new String[]{
                        COLOR.GOLD.getValue() + "玩家: " + COLOR.WHITE.getValue() + target.getName(),
                        COLOR.GOLD.getValue() + "世界: " + COLOR.WHITE.getValue() + location.getWorld().getName(),
                        COLOR.GOLD.getValue() + "坐标: " + COLOR.WHITE.getValue() + (int) location.getX() + " , " + (int) location.getY() + " , " + (int) location.getZ(),
                });
                return true;
            case "enderchest":
                target = player;
                if (args.length > 1 && isAdmin)
                    target = Bukkit.getPlayer(args[1]);
                if (target == null) {
                    UTIL.SEND(sender, COLOR.RED.getValue() + "操作失败，目标不存在。");
                    return true;
                }
                player.openInventory(target.getEnderChest());
                return true;
            case "inventory":
                if (isAdmin) {
                    target = player;
                    if (args.length > 1 && isAdmin){
                        target = Bukkit.getPlayer(args[1]);
                    }
                    if (target == null) {
                        UTIL.SEND(sender, COLOR.RED.getValue() + "操作失败，目标不存在。");
                        return true;
                    }
                    player.openInventory(target.getInventory());
                    return true;
                }
                UTIL.SEND(sender, COLOR.RED.getValue() + "没有使用该指令的权限。");
                return true;
            case "list":
                if (isAdmin) {
                    TextComponent message = new TextComponent("");
                    for (Player online : Bukkit.getServer().getOnlinePlayers()) {
                        TextComponent fix = new TextComponent(COLOR.WHITE.getValue() + " | ");
                        TextComponent ec = new TextComponent(COLOR.PINK.getValue() + "※ ");
                        ec.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(COLOR.PINK.getValue() +"点击打开该玩家末影箱").create()));
                        ec.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/r enderchest "+online.getName()));
                        TextComponent inv = new TextComponent(COLOR.GOLD.getValue() + " ※");
                        inv.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(COLOR.GOLD.getValue() +"点击打开该玩家物品栏").create()));
                        inv.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/r inventory "+online.getName()));
                        TextComponent name = new TextComponent(COLOR.GREEN.getValue() + online.getName());
                        name.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(COLOR.GREEN.getValue()+"UUID:"+COLOR.WHITE.getValue()+String.valueOf(online.getUniqueId())+"\n"+COLOR.GREEN.getValue()+"点击复制玩家UUID").create()));
                        name.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, online.getUniqueId().toString()));
                        message.addExtra(fix);
                        message.addExtra(ec);
                        message.addExtra(name);
                        message.addExtra(inv);
                    }
                    for (OfflinePlayer offline : Bukkit.getServer().getOfflinePlayers()) {
                        TextComponent fix = new TextComponent(COLOR.WHITE.getValue() + " | ");
                        TextComponent name = new TextComponent(COLOR.RED.getValue() + offline.getName());
                        name.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(COLOR.RED.getValue()+"UUID:"+COLOR.WHITE.getValue()+String.valueOf(offline.getUniqueId())+"\n"+COLOR.RED.getValue()+"点击复制玩家UUID").create()));
                        name.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, offline.getUniqueId().toString()));
                        message.addExtra(fix);
                        message.addExtra(name);
                    }
                    if (!message.equals(new TextComponent("")))
                        message.addExtra(new TextComponent(COLOR.WHITE.getValue() + " | "));
                    sender.spigot().sendMessage(message);
                    return true;
                }
                UTIL.SEND(sender, COLOR.RED.getValue() + "没有使用该指令的权限。");
                return true;
        }
        UTIL.SEND(sender, COLOR.RED.getValue() + "指令不存在或未启用。");
        return true;
    }

    private String getCmd(String str) {
        List<String[]> list = new ArrayList() {{
            add(new String[]{"?", "help"});
            add(new String[]{"!", "menu"});
            add(new String[]{"time", "clock"});
            add(new String[]{"pos", "position"});
            add(new String[]{"ec", "enderchest"});
            add(new String[]{"inv", "inventory"});
            add(new String[]{"list"});
        }};
        for (String[] strs : list)
            if (UTIL.CHECK(str, strs))
                return strs[strs.length - 1];
        return "";
    }

    public CMDMain() {
    }

    public CMDMain(FileConfiguration config) {
        this.conf = new HashMap() {{
        }};
    }

    public CMDMain(JavaPlugin plugin, FileConfiguration config) {
        this(config);
        plugin.getCommand("r").setTabCompleter(new CMPMain());
        Bukkit.getPluginManager().registerEvents(new LSNMain(), plugin);

    }

    public Integer setTime(int time) {
        int h = time / 100;
        int m = time % 100;
        return (int) (h - 6 > 0 ? (h - 6) * 1000 : (h + 18) * 1000 + m / 0.06);
    }

    public Integer getTime() {
        int time = Integer.parseInt(String.valueOf(Bukkit.getWorld("world").getTime()));
        int h = time / 1000 + 6 > 23 ? time / 1000 - 18 : time / 1000 + 6;
        Double m = time % 1000 * 0.06;
        return (int) (h * 100 + m);
    }
}
